Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First implementation for two endpoints. #1

Merged
merged 10 commits into from
Sep 25, 2024
Merged

First implementation for two endpoints. #1

merged 10 commits into from
Sep 25, 2024

Conversation

sgmv
Copy link
Contributor

@sgmv sgmv commented Aug 23, 2024

First implementation for SD3.

.gitignore Outdated Show resolved Hide resolved
@@ -0,0 +1,13 @@
default: test

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add for CI:

SHELL=/bin/bash

export PATH:=/usr/local/go/bin:~/go/bin/:$(PATH)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the SHELL variable, but PATH under discussion :) We will use actions, so maybe we don't need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, lint checks work fine without additional PATH. I'll check it with unit tests.

internal/app/app.go Outdated Show resolved Hide resolved
"github.com/gin-gonic/gin"
)

func ErrorHandle() gin.HandlerFunc {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think better to make like this:

var (
	// ErrInternal is an error for when an internal service fails to process the request
	ErrInternal = errors.New("internal error")
	// ErrDataNotFound is an error for when requested data is not found
	ErrDataNotFound = errors.New("data not found")
	// ErrNoUpdatedData is an error for when no data is provided to update
	ErrNoUpdatedData = errors.New("no data to update")
	// ErrConflictingData is an error for when data conflicts with existing data
	ErrConflictingData = errors.New("data conflicts with existing data in unique column")
	// ErrInvalidCredentials is an error for when the credentials are invalid
	ErrInvalidCredentials = errors.New("invalid email or password")
	// ErrUnauthorized is an error for when the user is unauthorized
	ErrUnauthorized = errors.New("user is unauthorized to access the resource")
)

var errorStatusMap = map[error]int{
        ErrInternal:                   http.StatusInternalServerError,
        ErrDataNotFound:               http.StatusNotFound,
        ErrConflictingData:            http.StatusConflict,
        ErrInvalidCredentials:         http.StatusUnauthorized,
        ErrUnauthorized:               http.StatusUnauthorized,
}

func HandleError(ctx *gin.Context, err error) {
	statusCode, ok := errorStatusMap[err]
	if !ok {
		statusCode = http.StatusInternalServerError
	}

	errRsp := errorResponse(errMsg)
	ctx.JSON(statusCode, errRsp)
}

func errorResponse(errMsgs []string) errorResponse {
	return errorResponse{
		Success:  false,
		Messages: errMsgs,
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the error file will be soon.

internal/conf/conf.go Show resolved Hide resolved
Copy link
Member

@Aloento Aloento left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should build on V2 to be compatible with the V1 API, not simply move Python code to Golang. If we are just moving code, we can merely use Cython to speed up the original SD2 or further optimize on top of SD2. - we could do better.
And this database design, like I said before, can lead to performance issues.

@sgmv
Copy link
Contributor Author

sgmv commented Sep 2, 2024

We should build on V2 to be compatible with the V1 API, not simply move Python code to Golang. If we are just moving code, we can merely use Cython to speed up the original SD2 or further optimize on top of SD2. - we could do better. And this database design, like I said before, can lead to performance issues.

Don't worry, unfortunately we can't change DB before changing backend, and we should keep backward compatibility first. We will do a lot with schemas a bit later. Please check all endpoints, requests, responses.

@Aloento
Copy link
Member

Aloento commented Sep 4, 2024

We should build on V2 to be compatible with the V1 API, not simply move Python code to Golang. If we are just moving code, we can merely use Cython to speed up the original SD2 or further optimize on top of SD2. - we could do better. And this database design, like I said before, can lead to performance issues.

Don't worry, unfortunately we can't change DB before changing backend, and we should keep backward compatibility first. We will do a lot with schemas a bit later. Please check all endpoints, requests, responses.

just write Database Migration Code ↓

https://github.com/Aloento/StatusDashboard/blob/master/Services/Status.cs#L21

@Aloento
Copy link
Member

Aloento commented Sep 9, 2024

About the Database

I agree with something Vladi once said, “Later is never.” So, I strongly recommend developing the new database structure now. We don't need to consider compatibility with the old backend because the new backend will run on a new database instance. We only need to perform a one-time data migration when going live, and this process is quite simple. Our test instance can directly use the migrated data for demonstration. The data in my SD3 C# version is migrated in real-time, and it already supports complete and rapid migration. If it’s too complicated to write in Golang, I can modify the C# migration code to adapt to our new backend since migration is a one-time task. I’ve attached my designed database structure, hoping it will help you all.

OpenAPI v2

Front-end requests are cached, and only a fine-grained API can maximize front-end caching.

/v2/Event

We do not call it an incident because a maintenance event is not an incident, so now we use Event.

GET /v2/Events

list all event ids
this is for page https://sd3.eco.tsi-dev.otc-service.com/History

GET /v2/Event/{id}

get event entity by id
this is for pages like https://sd3.eco.tsi-dev.otc-service.com/Event/205

with related region ids, related component ids
not contain the update data

GET /v2/Event/{id}/Updates

Return all update entities of the event

GET /v2/Events/Active

return all Current Events ids
this is for home page component https://sd3.eco.tsi-dev.otc-service.com/#:~:text=working%20on%20it.-,Current%20Events,-ID

POST /v2/Event

Create a new event

PATCH /v2/Event/{id}

Update event by patch method and push new update entity at the same time

The main difference between PUT and PATCH in the REST API is that PUT completes the update operation by replacing the entire entity. In contrast, PATCH only updates the fields you provide and doesn't change any other values. If you use the PUT method, the entire content of the resource you update is updated.

POST /v2/Event/{id}/Split

by given the [{Service_Id}, {Region_Id}] pairs, split the event into two events.

POST /v2/Event/Merge

Merge two events into one event by providing the two event IDs, new event titles, descriptions, etc.

/v2/Region

GET /v2/Regions

List all region entities

Not contain any component data or event data or category data

GET /v2/Region/{id}/Categories

Return all categories ids present in the region

/v2/Category

GET /v2/Categories

List all categories entity

Not contain any component data or event data

GET /v2/Category/{id}/Services

Return all component ids present in the category

/v2/Service

GET /v2/Services

list all component ids

GET /v2/Service/{id}

get component entity by id
with related category id, and related region ids

GET /v2/Service/{id}/Events

Return all event ids present in the component

/v2/Availability

GET /v2/Availability/{Service_Id}/{Region_Id}

return the availability of the component in the last 6 months
like [100, 42.24, 70.44, ...]

@Aloento
Copy link
Member

Aloento commented Sep 9, 2024

image

@sgmv sgmv merged commit 52c0fbd into main Sep 25, 2024
2 checks passed
@sgmv sgmv deleted the arch-skeleton branch September 25, 2024 09:20
package app

const (
v1Group = "v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/api/v1 is mandatory

- name: components
description: Operations about components
paths:
/v1/components:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought /api was implemented elsewhere, but it's not.
So it has to be modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants